#include "SignCom.h"
#include "Serial.h"
#include <iostream>
#include <string>
using namespace std;


const char WIDE = 0x1C;
const char FLASH = 0x0F;
const char CENTRE = 0x02;
const char JUMP = 0x09;
const char UP = 0x0B;
const char DOWN = 0x0E;
const char BLINK = 0x1D;
const char OPEN = 0x1E;
const char WIPE = 0x1F;
const char FREEZE = 0x1B;
const char PAUSE = 0x10;
const char TIME = 0x16;
const char TEMP = 0x15;

const char commands[] = {0x1C,0x0F,0x02,0x09,0x0B,0x0E,0x1D,0x1E,0x1F,0x1B,0x10,0x16,0x15};

CSignCom::CSignCom(void)
{
}

CSignCom::~CSignCom(void)
{
}


void CSignCom::DisplayLotNumber(CSerial* aSerial, const char* aString, bool aFinished)
{
	//The lot number will be between 1-4 character...so fill the string with spaces.
	std::string inputStr(aString);
	int size = inputStr.size();
	if (0 == size)
		return;

	int i = 0;
	for (i; i < (6-size); i++)
		inputStr.insert(0,1,' ');

	if(!aFinished)
	{
		inputStr.insert(i,1,FLASH);
		inputStr.insert(i+2,1,FLASH);
	}

	inputStr.insert(0,1,FREEZE);

	DisplayString(aSerial, inputStr.c_str(), -1);
}


void CSignCom::DisplayString(CSerial* aSerial, const char* aString, int aCommand)
{
	if (aString && aSerial && aSerial->IsOpened())
	{
		//char size = (char)strlen(aString);
		std::string signStr;
		std::string inputStr(aString);

		if(inputStr.size()<=0)
			return;
		if(aCommand >0 && aCommand < 13)
			inputStr.insert(0,1,commands[aCommand]);


		char size = (char)inputStr.size();
		signStr.append(1, 0x82);
		signStr.append(1, 0x00);
		signStr.append(1, 0x00);
		signStr.append(1, 0x03);
		signStr.append(1, 0x00);


    if(!inputStr.compare("START"))
    {
        inputStr.clear();
        inputStr.append(1,'V');
        inputStr.append(1, 0x7B);
        inputStr.append("lkommen till LIONS lotteri");
        inputStr.append(1, OPEN);
        inputStr.append(1, TIME);

        inputStr.append(1, UP);

        inputStr.append(1, TIME);
        size = inputStr.size();
    }


		signStr.append(1, (size+1));
		signStr.append(inputStr);

		char checksum = 0;
		for(int i=0; i<size; i++)
		{
			checksum += inputStr[i];
		}
		signStr.append(1, checksum);

		aSerial->SendData(signStr.c_str(),signStr.size());
	}
	else
	{
		cout << "ERROR: CSignCom::DisplayString()" << endl;
	}
}
